package com.puzzlebazar.client.squaregrid.view;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.puzzlebazar.client.util.BoundInfo;
import com.puzzlebazar.client.util.NumberFilter;

public class SizeDialog extends DialogBox {

	public interface Callback {
		public void ok( int width, int height );
		public void cancel();
	}

	private int lastValidWidth;
	private int lastValidHeight;	
	
	public SizeDialog( int width, int height, 
			final BoundInfo info, final Callback callback ) throws Exception {
		
		if( !info.isValidX( width ) || !info.isValidY( height ) )
			throw new Exception( "Invalid initial size" );		
		
		lastValidWidth = width;
		lastValidHeight = height;		
		
		// Set the dialog box's caption.
		setText("Enter grid size");

		final VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setWidth( "100%" );

		final Grid grid = new Grid();
		grid.setWidth( "100%" );
		grid.setCellPadding( 0 );
		grid.setCellSpacing( 3 );
		grid.resize( 2, 3 );
		grid.getColumnFormatter().setWidth(0, "55px");
		grid.getColumnFormatter().setWidth(1, "26px");
		
		final HorizontalPanel buttonPanel = new HorizontalPanel();
		
		mainPanel.add( grid );
		mainPanel.add( buttonPanel );
		
		final Label widthLabel  = new Label( "Width :" );	      
		final Label heightLabel = new Label( "Height :" );
		final Label cellsLabel1 = new Label( "cells ");
		final Label cellsLabel2 = new Label( "cells ");
		
		final TextBox widthText = new TextBox();
		widthText.setWidth("26px");
		widthText.setMaxLength(2);
		widthText.setText( Integer.toString(width) );
		widthText.addKeyPressHandler( new NumberFilter() );
		widthText.addChangeHandler( new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				boolean valid = false;
				try {
					int value = Integer.parseInt( widthText.getText() );
					if( info.isValidX(value) ) {
						lastValidWidth = value;
						valid = true;
					}
				}
				catch( NumberFormatException exception ) {
				}
				if( !valid ) {
					widthText.setText( Integer.toString(lastValidWidth) );
				}
			}			
		});
		
		final TextBox heightText = new TextBox();
		heightText.setWidth("26px");
		heightText.setMaxLength(2);
		heightText.setText( Integer.toString(height) );
		heightText.addKeyPressHandler( new NumberFilter() );
		heightText.addChangeHandler( new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				boolean valid = false;
				try {
					int value = Integer.parseInt( heightText.getText() );
					if( info.isValidY(value) ) {
						lastValidHeight = value;
						valid = true;
					}
				}
				catch( NumberFormatException exception ) {
				}
				if( !valid ) {
					heightText.setText( Integer.toString(lastValidHeight) );
				}
			}			
		});

		grid.setWidget( 0, 0, widthLabel );
		grid.setWidget( 0, 1, widthText );
		grid.setWidget( 0, 2, cellsLabel1 );

		grid.setWidget( 1, 0, heightLabel );
		grid.setWidget( 1, 1, heightText );
		grid.setWidget( 1, 2, cellsLabel2 );


		final Button ok = new Button("OK");
		ok.setWidth( "75px" );
		ok.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.ok( lastValidWidth, lastValidHeight );
				SizeDialog.this.hide();
			}
		});
		
		final Button cancel = new Button("Cancel");
		cancel.setWidth( "75px" );
		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.cancel();
				SizeDialog.this.hide();
			}
		});
		
		buttonPanel.add( ok );
		buttonPanel.setCellWidth( ok, "80px" );
		buttonPanel.add( cancel );
		buttonPanel.setCellWidth( cancel, "80px" );

		setWidget(mainPanel);		
		setAnimationEnabled(true);
		
		heightText.addKeyPressHandler( new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				char keyCode = event.getCharCode();
				if( keyCode == KeyCodes.KEY_ENTER && ok.isEnabled() )
					ok.click();
			}						
		} );
		
		DeferredCommand.addCommand( new Command() {
			@Override
			public void execute() {
				widthText.setFocus( true );
				widthText.selectAll();
			}			
		});
				
	}
}
